#line 1 "C:/Documents and Settings/Administrator/My Documents/TFC/trunk/spi.c"







sbit PORT_SDI at RC5_bit;
sbit PORT_SDO at RC4_bit;
sbit PORT_SCLK at RC3_bit;
sbit PORT_CS at RC2_bit;

sbit TRIS_SDI at TRISC5_bit;
sbit TRIS_SDO at TRISC4_bit;
sbit TRIS_SCLK at TRISC3_bit;
sbit TRIS_CS at TRISC2_bit;
#line 34 "C:/Documents and Settings/Administrator/My Documents/TFC/trunk/spi.c"
int spiXfer(int spiDirection, char *pSpiData);
#line 46 "C:/Documents and Settings/Administrator/My Documents/TFC/trunk/spi.c"
void spiDelay() {
 asm nop
 asm nop
 asm nop
 asm nop
}









int spiXfer(int spiDirection, char *pSpiData)
{
 unsigned char retData;
 unsigned char bitData;


 PORT_CS = 1;


 PORT_SDI = 1;

 spiDelay();
 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;


 PORT_SDI = spiDirection;

 spiDelay();
 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;


 PORT_SDI = 0;

 spiDelay();
 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;


 bitData = 0x80;
 if (spiDirection)
 {

 retData = 0;

 while (bitData)
 {
 spiDelay();

 if (PORT_SDO) retData |= bitData;
 else retData |= 0;

 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;
 bitData = bitData >> 1;
 }

 *pSpiData = retData;
 }
 else
 {

 retData = *pSpiData;

 while (bitData)
 {
 PORT_SDI = ( retData & bitData ) ? 1 : 0;
 spiDelay();
 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;
 bitData = bitData >> 1;
 }
 }


 spiDelay();
 bitData = PORT_SDO;
 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;


 PORT_CS = 0;


 spiDelay();
 PORT_SCLK = 1;
 spiDelay();
 PORT_SCLK = 0;

 return bitData;
}
#line 161 "C:/Documents and Settings/Administrator/My Documents/TFC/trunk/spi.c"
void spiInit()
{

 TRIS_SDO = 1;
 TRIS_SDI = 0;
 TRIS_SCLK = 0;
 TRIS_CS = 0;




 PORT_SDI = 0;

 PORT_SCLK = 0;

 PORT_CS = 0;
}








char spiReadWait()
{
 char spiData;
 while (spiXfer( 1 , &spiData));
 return spiData;
}









int spiRead(char *pSpiData)
{
 return spiXfer( 1 , pSpiData);
}








void spiWrite(char spiData)
{
 while (spiXfer( 0 , &spiData));
}










void CmdSend(char Cmd, unsigned char Param)
{
 spiWrite(Cmd);
 spiWrite(' ');
 spiWrite(Param);
 spiWrite(0x0d);
 }
